package com.cv.platform.service.message;

import cn.hutool.core.util.ObjUtil;
import com.cv.framework.common.utils.object.BeanUtils;
import com.cv.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.cv.framework.security.core.utils.SecurityFrameworkUtil;
import com.cv.platform.controller.platform.message.vo.conversation.CvConversationVisibleSaveReqVO;
import com.cv.platform.dal.dataobject.message.CvConversationVisibilityDO;
import com.cv.platform.dal.mapper.message.CvConversationVisibilityMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 消息会话可见业务层接口实现类
 *
 * @author Charles_XDXD
 */
@Service
public class CvConversationVisibleServiceImpl implements CvConversationVisibleService {

    @Resource
    private CvConversationVisibilityMapper cvConversationVisibilityMapper;

    @Override
    public void saveConversationVisible(CvConversationVisibleSaveReqVO reqVO) {
        CvConversationVisibilityDO visibilityDO = BeanUtils.toBean(reqVO, CvConversationVisibilityDO.class);
        visibilityDO.setIsVisible(1);
        cvConversationVisibilityMapper.insert(visibilityDO);
    }

    @Override
    public boolean isVisible(Long conversationId) {
        Long userId = SecurityFrameworkUtil.getLoginUserId();
        LambdaQueryWrapperX<CvConversationVisibilityDO> wrapper = new LambdaQueryWrapperX<CvConversationVisibilityDO>()
                .eq(CvConversationVisibilityDO::getConversationId, conversationId)
                .eq(CvConversationVisibilityDO::getUserId, userId);

        CvConversationVisibilityDO cvConversationVisibilityDO = cvConversationVisibilityMapper.selectOne(wrapper);
        if (ObjUtil.isNotEmpty(cvConversationVisibilityDO)) {
            return cvConversationVisibilityDO.getIsVisible() == 1;
        }

        return false;
    }

    @Override
    public void setVisible(Long conversationId, boolean visible) {
        Long userId = SecurityFrameworkUtil.getLoginUserId();
        LambdaQueryWrapperX<CvConversationVisibilityDO> wrapper = new LambdaQueryWrapperX<CvConversationVisibilityDO>()
                .eq(CvConversationVisibilityDO::getConversationId, conversationId)
                .eq(CvConversationVisibilityDO::getUserId, userId);
        CvConversationVisibilityDO cvConversationVisibilityDO = cvConversationVisibilityMapper.selectOne(wrapper);

        if (ObjUtil.isEmpty(cvConversationVisibilityDO)) {
            cvConversationVisibilityDO = new CvConversationVisibilityDO();
        }
        
        cvConversationVisibilityDO
                .setConversationId(conversationId)
                .setIsVisible(visible ? 1 : 0)
                .setConversationId(conversationId);
        cvConversationVisibilityMapper.insertOrUpdate(cvConversationVisibilityDO);
    }

    @Override
    public void setVisible(Long conversationId, Long userId, boolean visible) {
        LambdaQueryWrapperX<CvConversationVisibilityDO> wrapper = new LambdaQueryWrapperX<CvConversationVisibilityDO>()
                .eq(CvConversationVisibilityDO::getConversationId, conversationId)
                .eq(CvConversationVisibilityDO::getUserId, userId);
        CvConversationVisibilityDO cvConversationVisibilityDO = cvConversationVisibilityMapper.selectOne(wrapper);

        cvConversationVisibilityDO
                .setConversationId(conversationId)
                .setIsVisible(visible ? 1 : 0)
                .setConversationId(conversationId);
        cvConversationVisibilityMapper.insertOrUpdate(cvConversationVisibilityDO);
    }

}
